/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.home

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.item_need.view.*
import top.stime21.framework.extension.toActivityWithSerializable
import top.stime21.myneighbours.R
import top.stime21.myneighbours.data.entity.Need
import top.stime21.myneighbours.module.details.NeedDetailsActivity
import top.stime21.myneighbours.utils.ImageLoader
import java.util.*

/**
 * Author: czmc  Email: zxc6355533@163.com
 * Version:	1.0  Date:2018/3/2
 */
class NeedAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private var context: Context? = null
    var data: List<Need>? = ArrayList<Need>()

    constructor(context: Context, data: List<Need>) {
        this.context = context;
        this.data = data;
    }

    constructor(context: Context) {
        this.context = context;
    }

    fun setUpdateData(d: List<Need>?) {
        data = d
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        val need: Need = data!!.get(position)
        holder!!.itemView.textView3.text = need.address
        holder!!.itemView.textView2.text = need.title
        holder!!.itemView.textView4.text = need.content
        when (need.status) {
            1 -> {
                holder!!.itemView.tv_status.setTextColor(context!!.resources.getColor(R.color.colorWaitting))
                holder!!.itemView.tv_status.text = "等待中"
            }
            2 -> {
                holder!!.itemView.tv_status.setTextColor(context!!.resources.getColor(R.color.colorDoing))
                holder!!.itemView.tv_status.text = "进行中"
            }
            3 -> {
                holder!!.itemView.tv_status.setTextColor(context!!.resources.getColor(R.color.colorFinish))
                holder!!.itemView.tv_status.text = "已完成"
            }
            4, 5 -> {
                holder!!.itemView.tv_status.setTextColor(context!!.resources.getColor(R.color.colorCancel))
                holder!!.itemView.tv_status.text = "已取消"
        }
    }


    if(need.user!=null)
    {
        if(need.user.avatar!=null)
            ImageLoader().loadRound(need.user.avatar, holder.itemView.imageView, 5);
        else{
          when (need.user.gender){
              1->holder.itemView.imageView.setImageResource(R.drawable.ic_male)
              2->holder.itemView.imageView.setImageResource(R.drawable.ic_female)
          }
        }
    }
    holder!!.itemView!!.setOnClickListener(
    {
        context!!.toActivityWithSerializable<NeedDetailsActivity>(need)
    })
}

override fun getItemCount(): Int {
    return data!!.size
}


override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
    return ItemViewHolder(LayoutInflater.from(context!!).inflate(R.layout.item_need, parent, false))
}

class ItemViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

}
}